﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Repository;
using System.Data.SqlClient;
using System.Data;

namespace Controller
{
    public class EnderecoController
    {

        private EnderecoDB enderecoDb = new EnderecoDB(ConnectionFactory.getConnetcion());
        private ClienteDB clienteDb = new ClienteDB(ConnectionFactory.getConnetcion());

        public void Connect(SqlConnection connection)
        {
            try
            {
                enderecoDb.putConnection(connection);
            }
            catch (SqlException sqlex)
            {
            }
        }

        public bool cadastrarEndereco(Cliente cliente) 
        {
            bool result;
            
            try 
            {
                Cliente c = new Cliente();

                enderecoDb.openConnection();
                result = enderecoDb.saveEnderecos(cliente);
                enderecoDb.closeConnection();
                return result;
            }catch(SqlException sqlex) {

                return false;
            }

        }


        public bool deletarEndereco(Cliente cliente, Endereco endereco)
        {
            bool result;

            try
            {
                endereco.Id_cliente = cliente.Id_cliente;
                IQuery<Endereco> consult = enderecoDb;
                consult.openConnection();
                result = consult.delete(endereco);
                consult.closeConnection();
                return result;
            }
            catch (SqlException sqlex)
            {
                return false;
            }

        }

        public bool atualizarEndereco(Cliente cliente, Endereco endereco)
        {
            bool result;

            try
            {
                endereco.Id_cliente = cliente.Id_cliente;
                IQuery<Endereco> consult = enderecoDb;
                consult.openConnection();
                result = consult.update(endereco);
                consult.closeConnection();
                return result;
            }
            catch (SqlException sqlex)
            {
                return false;
            }

        }

        public DataTable listaEnderecoPorCliente() 
        {
            DataTable dt;
            enderecoDb.openConnection();
            dt = enderecoDb.AuxEnderecoGrid();
            enderecoDb.closeConnection();
            return dt;
        }

        public List<Endereco> listarEndereco()
        {

            try
            {
                List<Endereco> enderecos = new List<Endereco>();
                IQuery<Endereco> consult = enderecoDb;
                consult.openConnection();
                consult.getAllList();
                consult.closeConnection();
                return enderecos;
            }
            catch (SqlException sqlex)
            {
                return null;
            }

        }

        public List<Endereco> listarEndereco(int id_cliente)
        {

            try
            {
                List<Endereco> enderecos = new List<Endereco>();
                IQuery<Endereco> consult = enderecoDb;
                consult.openConnection();
                enderecos = consult.getListById(id_cliente);
                consult.closeConnection();
                return enderecos;
            }
            catch (SqlException sqlex)
            {
                return null;
            }

        }

    }
}
